//DOM渲染完成
$(function () {
    //幻灯片
    var $editTeacherName = $(".editTeacherName"),
        $editTeacherGender = $(".editTeacherGender"),
        $editTeacherAge = $(".editTeacherAge"),
        $editTeacherTitle = $(".editTeacherTitle"),
        $editTeacherComment = $(".editTeacherComment"),
        $editTeacherImg = $(".editTeacherImg");
    //上传图片
    function upImgAjax(config) {
        EasyAjax.ajax_Upload_File({
                url: "file/slideUpload.json",
                data: config.fileData
            },
            function (data) {
                $editTeacherImg.attr("src",api_host+data.data);
                $editTeacherImg.attr("data-src",data.data);
            });
    }
    //上传图片按钮
    $(".editUpImgFile").on("change",function(){
        var file=$(".editUpImgFile").get(0).files[0];
        var fileData=new FormData();
        fileData.append("upImg",file);
        $(".editUpImgFile").val("");
        upImgAjax({
            fileData:fileData
        });
    });
    //初始化幻灯片
    function initTeacher() {
        EasyAjax.ajax_Get_Json({
                url: "teacher/getTeacherList.json"
            },
            function (data) {
                $.each(data.data,function (index,ele) {
                    this.api_host = api_host;
                });
                var $teacher = $("#teacherTpl").render(data.data);
                $(".showTeacherList").html($teacher);
            });
    }
    initTeacher();
    //修改按钮
    var editTeacherUid = "";
    $(document).on("click",".editTeacherBtn",function () {
        var showTeacherName = $(this).parents("tr:first").find(".showTeacherName").html(),
            showTeacherGender = $(this).parents("tr:first").find(".showTeacherGender").html(),
            showTeacherAge = $(this).parents("tr:first").find(".showTeacherAge").html(),
            showTeacherTitle = $(this).parents("tr:first").find(".showTeacherTitle").html(),
            showTeacherComment = $(this).parents("tr:first").find(".showTeacherComment").html(),
            showTeacherImg = $(this).parents("tr:first").find(".showTeacherImg").attr("data-src");
        editTeacherUid = $(this).parents("tr:first").attr("uid");
        $editTeacherName.val(showTeacherName);
        $editTeacherGender.val(showTeacherGender);
        $editTeacherAge.val(showTeacherAge);
        $editTeacherTitle.val(showTeacherTitle);
        $editTeacherComment.val(showTeacherComment);
        $editTeacherImg.attr('src',api_host+showTeacherImg);
        $editTeacherImg.attr('data-src',showTeacherImg);
        layer.open({
            type: 1,
            title: "编辑老师",
            shadeClose: true,
            padding:10,
            area: ['700px', '630px'],
            content: $('.editTeacherTpl')
        });
    });
    //添加按钮
    $(".addTeacherBtn").on("click",function () {
        editTeacherUid = "";
        $editTeacherName.val("");
        $editTeacherGender.val("");
        $editTeacherAge.val("");
        $editTeacherTitle.val("");
        $editTeacherComment.val("");
        $editTeacherImg.attr('src',"");
        $editTeacherImg.attr('data-src',"");
        layer.open({
            type: 1,
            title: "新增老师",
            shadeClose: true,
            padding:10,
            area: ['700px', '630px'],
            content: $('.editTeacherTpl')
        });
    });
    //编辑幻灯片
    $(".saveEditTeacher").on("click",function () {
        var editTeacherName = $editTeacherName.val(),
            editTeacherGender = $editTeacherGender.val(),
            editTeacherAge = $editTeacherAge.val(),
            editTeacherTitle = $editTeacherTitle.val(),
            editTeacherComment = $editTeacherComment.val(),
            editTeacherImg = $editTeacherImg.attr("data-src"),
            $this = $(this);
        if(editTeacherUid){
            //修改幻灯片
            EasyAjax.ajax_Post_Json({
                    url: "teacher/updateTeacher.json",
                    data: {editTeacherUid:editTeacherUid,editTeacherName:editTeacherName,editTeacherGender:editTeacherGender,editTeacherAge:editTeacherAge,editTeacherTitle:editTeacherTitle,editTeacherComment:editTeacherComment,editTeacherImg:editTeacherImg}
                },
                function (data) {
                    initTeacher();
                    $this.parents(".layui-layer-page").find(".layui-layer-close").click();
                    layer.msg("修改成功！");
                });
        }else{
            //添加幻灯片
            EasyAjax.ajax_Post_Json({
                    url: "teacher/addTeacher.json",
                    data: {setTeacherName:editTeacherName,setTeacherGender:editTeacherGender,setTeacherAge:editTeacherAge,setTeacherTitle:editTeacherTitle,setTeacherComment:editTeacherComment,setTeacherImg:editTeacherImg}
                },
                function (data) {
                    initTeacher();
                    $this.parents(".layui-layer-page").find(".layui-layer-close").click();
                    layer.msg("添加成功！");
                });
        }
    });
    //删除幻灯片
    $(document).on("click",".delTeacherBtn",function () {
        editTeacherUid = $(this).parents("tr:first").attr("uid");
        layer.confirm('确认删除', {
            btn: ['确认','取消'],
            anim: 6
        }, function(){
            EasyAjax.ajax_Post_Json({
                    url: "teacher/delTeacher.json",
                    data: {delTeacherUid:editTeacherUid}
                },
                function (data) {
                    initTeacher();
                    layer.msg("删除成功！");
                });
        }, function(){

        });
    });
});